import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

/** 
 * Program take 4 command line arguments.
 * 1. File name one
 * 2. File name two
 * 3. Expression of type "string1=string2"
 * Application read file one and write its content to file two replacing all entries of string1 with string2.
 * @author me
 * */
public class ParserEasy2Files {
    
    public static void main(String[] args){
        if (args.length < 4)
            System.out.print("You have entered the wrong parameters");
        else {
            String path = args[0];
            String path1 = args[1];
            String str = args[2];
            String str1 = args[3];
            readerFile(path, path1, str, str1);
        }
        }
/** 
 * Method readerFile read file 1 line by line and call methods changeString and writerFile
 * */
    
    public static void readerFile(String nameFile1, String nameFile2, String strFind, String strCh) {
        try {
            File f = new File(nameFile1);
            FileInputStream is = new FileInputStream(f);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            ArrayList<String> word = new ArrayList<String>();
            String str;
            while ((str = reader.readLine()) != null) {
                   String strRepl = changeString(str, strFind, strCh);
                   word.add(strRepl);
                   writerFile(nameFile2, strRepl); 
                   }
            show(word);
            //System.out.println(word.get(0));
            //System.out.println(word.size());
            reader.close();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace(System.err);
            
        } catch (IOException e) {           
            e.printStackTrace(System.err);
        }
        }

    /** 
     * Method changeString change substring in current string of file 1
     * */    
    
       public static String changeString(String str, String findStr, String replStr){
           String result = str;
           if (result != null && result.length() > 0) {
               int a = 0;
               int b = 0;
               while (true) {
                   a = result.indexOf(findStr, b);
                   if (a != -1) {
                       result = result.substring(0, a) + replStr + result.substring(a + findStr.length());
                       b = a + replStr.length();
                   } else
                       break;
               }
           }
           return result;

           
       }
       
       /** 
        * Method writerFile write modified string in file 2 
        * */    
       
       public static void writerFile (String file, String str) {
           PrintWriter out = null;
           try {
            out = new PrintWriter(new FileWriter(file, true));
            out.println(str);
            
        } catch (FileNotFoundException e) {
            System.out.println("File is not found");        
            } 
        catch (IOException e) {           
            e.printStackTrace(System.err);
        }
        finally {
            if (out != null)
                out.close();
        }
        }
       
       /** 
        * Method show is not specified in this task, but I it is help me to control the process:) 
        * */ 
       
       public static void show(ArrayList<String> array){
           System.out.println("Array : ");
                   for (String r : array) {
                   System.out.println(r);
                   }
               }

       }
